﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Dapper;
using Bird.OAWeb.EF;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using Bird.OAWeb.Domain.DTO;
using Bird.OAWeb.Domain.Table;
using Bird.OAWeb.Service.IService;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Authorization;
using WireMock.Admin.Mappings;

using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace Bird.OAWeb.WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//授权特性,表示：下面的控制器中所有的方法，都必须登录之后访问
    public class UserController : ControllerBase
    {
        IUserService se; 
        public UserController(IUserService se)
        {
            this.se = se;
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Logginadd(User obj)
        {
            return Ok(se.Logginadd(obj));
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="use"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]//允许匿名访问
        
        [NoLog]
        public IActionResult Loggin(UserDTO use)
        {
            var user = se.Loggin(use);
            if (user == null)
            {
                return Ok("400");
            }
            List<Claim> claims = new List<Claim>()
            {
                new Claim(ClaimTypes.Name,use.UserName),
                new Claim(ClaimTypes.NameIdentifier,use.UserId.ToString())
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("420116200402116614"));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            DateTime ex = DateTime.Now.AddSeconds(2*3600);
            var jwtToken = new JwtSecurityToken(
                issuer:"LY",
                audience:"everyone",
                claims:claims,
                expires:ex,
                signingCredentials:credentials
                );
            string strToken=new JwtSecurityTokenHandler().WriteToken(jwtToken);
            return Ok(strToken);
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult UserDelete(int id)
        {
            return Ok(se.UserDelete(id));
        }
        /// <summary>
        /// 用户反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult  UserBind(int id)
        {
            return Ok(se.UserBind(id));
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult  UserUpdate(User model)
        {
            return Ok(se.UserUpdate(model));
        }
    }
}
